package com.skyworth.web.wechat.engin;

import com.alibaba.fastjson.JSON;
import com.skyworth.utils.RequestUtil;
import com.skyworth.utils.StrUtil;
import com.skyworth.web.wechat.common.MessageParse;
import com.skyworth.web.wechat.entity.WxUserInfoModel;
import com.skyworth.web.wechat.protocol.BaseButton;
import com.skyworth.web.wechat.protocol.ScanPayReq;
import org.apache.catalina.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Created by yuhaichao on 2016/8/24.
 */
public class ServerEngin {

    private static final Logger log = LoggerFactory.getLogger(ServerEngin.class);
    private static ServerEngin serverEngin = new ServerEngin();

    private ServerEngin(){

    }

    public static ServerEngin getInstance() {
        return serverEngin;
    }

    public Map<Object,Object> unifiedOrder(ScanPayReq sp){
        try {
            String payXml = MessageParse.payMessageToXml(sp);
            log.info("payXml:"+payXml);
            String returnPayStr = RequestUtil.httpsRequest("https://api.mch.weixin.qq.com/pay/unifiedorder", "POST", payXml);
            log.info("returnPayStr:"+returnPayStr);
            Map<Object,Object> payReturnMap = MessageParse.parseXmlByString(returnPayStr);
            return payReturnMap;
        }catch(Exception e){
            log.error(e.getMessage());
            return null;
        }
    }

    public Map<Object,Object> getAccessToken(String appId,String appSecret){
            String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appId + "&secret=" + appSecret;
            String result = RequestUtil.httpsRequest(url, "GET", null);
            if (result != null && !"".equals(result)) {
                Map<Object, Object> map = (Map<Object, Object>) JSON.parse(result);
                return map;
            }
        return null;
    }

    public Map<Object,Object> getTicket(String accessToken){
            String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + accessToken + "&type=jsapi";
            String result = RequestUtil.httpsRequest(url, "GET", null);
            if (result != null && !"".equals(result)) {
                Map<Object, Object> map = (Map<Object, Object>) JSON.parse(result);
                return map;
            }

        return null;
    }

    public Map<Object,Object> getOauthUserId(String code,String appId,String appSecret){
            String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appId+"&secret="+appSecret+"&code="+code+"&grant_type=authorization_code";
            String result = RequestUtil.httpsRequest(url, "GET", null);
            if (result != null && !"".equals(result)) {
                Map<Object, Object> map = (Map<Object, Object>) JSON.parse(result);
                return map;
            }
        return null;

    }

    public Map<Object,Object> getUserInfo(String accessToken,String openid){
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token="+accessToken+"&openid="+openid+"&lang=zh_CN";
        log.info("url:"+url);
        String result = RequestUtil.httpsRequest(url, "GET", null);
        log.info("result:"+result);
        if (result != null && !"".equals(result)) {
            Map<Object,Object> m = (Map<Object, Object>) JSON.parse(result);
                if(m!=null){
                    String sex= StrUtil.objectToString(m.get("sex"));
                    m.put("sex", sex);
                    m.put("tagid_list", "");
                }
            return m;
        }
        return null;
    }

    public String createMenu(String accessToken,BaseButton button){
        String url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+accessToken;
        String dataStr = JSON.toJSONString(button);
        System.out.println("button:"+dataStr);
        String jsonStr=RequestUtil.httpsRequest(url, "POST", dataStr);
        System.out.println("createMenuJsonStr:"+jsonStr);
        return jsonStr;
    }

    public Map<Object,Object> getQrcInfo(String accessToken,String sceneStr){
        try {
            String url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=" + accessToken;
            String dataStr = "{\"action_name\": \"QR_LIMIT_STR_SCENE\", \"action_info\": {\"scene\": {\"scene_str\": \"" + sceneStr + "\"}}}";
            String jsonStr = RequestUtil.httpsRequest(url, "POST", dataStr);
            log.info("jsonStr:"+jsonStr);
            Map<Object, Object> map = (Map<Object, Object>) JSON.parse(jsonStr);
            return map;
        }catch(Exception e){
            log.error(e.getMessage());
        }
        return null;
    }

    public void sendTemplateMsg(String accessToken,Map<Object,Object> map){
        try{
            String sendurl="https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken;
            String requestJson = JSON.toJSONString(map);
            String returnStr=RequestUtil.httpsRequest(sendurl, "POST", requestJson);
            log.info("templateMsgReturn:"+returnStr);
        }catch(Exception e){
            log.error(e.getMessage());
        }
    }

}
